package com.zwj.service.impl;

import com.zwj.mapper.UserMapper;
import com.zwj.service.AsyncService;
import com.zwj.vo.entity.Report;
import com.zwj.vo.entity.User;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * @author snake
 * @version 1.0
 * @createTime 2025/8/3
 */
@Service
public class AsyncServiceImpl implements AsyncService {
    @Resource
    private UserMapper userMapper;

    /**
     * 异步生成用户报告
     *
     * @param userId 用户ID，用于标识需要生成报告的用户
     * @return CompletableFuture<Report> 异步返回生成的报告对象
     */
    @Async("taskExecutor")
    @Override
    public CompletableFuture<Report> generateReportAsync(Long userId) {
        System.out.println("开始生成报告，线程: " + Thread.currentThread().getName());

        // 模拟耗时的报告生成操作
        try {
            User user = userMapper.selectById(1L);
            System.out.println("用户信息: " + user);
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        Report report = new Report(userId, "报告内容...");
        return CompletableFuture.completedFuture(report);
    }

}
